{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.10 64-bit ('geo-env': conda)"
  },
  "interpreter": {
   "hash": "7b0d6b4aa929793d88e84d754c187056723d52cecd68845c7dca810866802715"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "import pandas as pd\r\n",
    "import matplotlib.pyplot as plt\r\n",
    "import numpy as np\r\n",
    "from osgeo import gdal\r\n",
    "gdal.AllRegister()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "# 读取TIFF遥感影像\r\n",
    "def read_img(filename):\r\n",
    "    dataset = gdal.Open(filename)\r\n",
    "    width = dataset.RasterXSize  # 栅格矩阵的列数\r\n",
    "    height = dataset.RasterYSize  # 栅格矩阵的行数\r\n",
    "    bands = dataset.RasterCount  # 波段数\r\n",
    "    geotrans = dataset.GetGeoTransform()  # 仿射矩阵，左上角像素的大地坐标和像素分辨率\r\n",
    "    proj = dataset.GetProjection()  # 地图投影信息，字符串表示\r\n",
    "    data = dataset.ReadAsArray(0, 0, width, height)\r\n",
    "    datatype = data.dtype\r\n",
    "    del dataset  # 关闭对象dataset，释放内存\r\n",
    "    return {\"width\":width, \"height\":height, \"data\":data,\"bands\":bands, \"geotrans\":geotrans, \"proj\":proj,\"datatype\":datatype}\r\n",
    "\r\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "read_img('\\demcode-learning\\dem\\Himalaya.tif')\r\n"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'width': 449,\n",
       " 'height': 410,\n",
       " 'data': array([[6167, 6188, 6208, ..., 6074, 6067, 6059],\n",
       "        [6142, 6163, 6182, ..., 6077, 6071, 6063],\n",
       "        [6113, 6135, 6152, ..., 6078, 6073, 6066],\n",
       "        ...,\n",
       "        [5166, 5159, 5153, ..., 5860, 5848, 5830],\n",
       "        [5178, 5170, 5161, ..., 5852, 5838, 5823],\n",
       "        [5188, 5179, 5169, ..., 5842, 5830, 5817]], dtype=int16),\n",
       " 'bands': 1,\n",
       " 'geotrans': (486387.9265206632, 30.0, 0.0, 3102000.726101742, 0.0, -30.0),\n",
       " 'proj': 'PROJCS[\"WGS 84 / UTM zone 45N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",87],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32645\"]]',\n",
       " 'datatype': dtype('int16')}"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "metadata": {}
  }
 ]
}